package com.demo.controller;

import cn.hutool.core.util.RandomUtil;
import cn.lingyangwl.framework.data.mate.fieldbind.FieldBindHandler;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.demo.entity.BigDataPO;
import com.demo.entity.StatusEnum;
import com.demo.fieldbind.model.FieldBindTestMainReq;
import com.demo.fieldbind.model.FieldBindTestReq;
import com.demo.fieldbind.model.FieldBindTestReq01;
import com.demo.mapper.BigDataMapper;
import com.demo.service.FieldBindTestService;
import org.apache.commons.lang3.time.StopWatch;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author shenguangyang
 */
@RequestMapping("/field_bind")
@RestController
public class FieldBindTestController {
    @Resource
    private BigDataMapper bigDataMapper;

    @Resource
    private FieldBindTestService fieldBindTestService;

    @Resource
    private FieldBindHandler fieldBindHandler;

    @GetMapping("/test")
    public void test5() {
//        testSingleObject();
//        testThreadRunSingleObject();
        testSingleObject02();
    }

    public void testSingleObject() {
        StopWatch stopWatch = StopWatch.createStarted();
        FieldBindTestReq fieldBindTestReq = FieldBindTestReq.initTest();
        fieldBindTestReq.setReq01(FieldBindTestReq01.initTest());
        fieldBindTestReq.setReq01s(new FieldBindTestReq01[] {FieldBindTestReq01.initTest()});
        fieldBindTestReq.setReq(FieldBindTestReq.initTest());
        fieldBindTestReq.setReq01List(Arrays.asList(FieldBindTestReq01.initTest()));
        fieldBindHandler.handleField(fieldBindTestReq);
        System.out.println("time: " + stopWatch.getTime(TimeUnit.MILLISECONDS) + " ms, " + JSON.toJSONString(fieldBindTestReq));
    }

    public void testSingleObject02() {
        StopWatch stopWatch = StopWatch.createStarted();
        FieldBindTestMainReq fieldBindTestReq = FieldBindTestMainReq.initTest();
        fieldBindTestReq.setReq01(FieldBindTestReq01.initTest());
        fieldBindTestReq.setReq01s(new FieldBindTestReq01[] {FieldBindTestReq01.initTest()});
        fieldBindTestReq.setReq(FieldBindTestMainReq.initTest());
        fieldBindTestReq.setReq01List(Arrays.asList(FieldBindTestReq01.initTest()));
        fieldBindHandler.handleField(fieldBindTestReq);
        System.out.println("time: " + stopWatch.getTime(TimeUnit.MILLISECONDS) + " ms, " + JSON.toJSONString(fieldBindTestReq));
    }

    public void testThreadRunSingleObject() {
        for (int i = 0; i < 20; i++) {
            new Thread(this::testSingleObject).start();
        }
    }

    /**
     * 直接调用字段绑定处理器
     */
    @GetMapping("/test1")
    public String test1() {
        for (int i = 0; i < 10; i++) {
            StopWatch allStopWatch = StopWatch.createStarted();
            BigDataPO bigDataPO = new BigDataPO();
            bigDataPO.setSex(0);
            bigDataPO.setStatus("0");
            fieldBindHandler.handleField(bigDataPO);
            System.out.println("time: " + allStopWatch.getTime() + " ms");
            System.out.println(bigDataPO);
            allStopWatch.reset();
        }
        return "ok";
    }

    /**
     * 查找数据库数据 -- 自动绑定字典数据
     */
    @GetMapping("/test2")
    public String test2() {
        BigDataPO bigDataPO = new BigDataPO();
        bigDataPO.setStatus(String.valueOf(RandomUtil.randomInt(0, 100) > 50 ? StatusEnum.online.getStatus() : StatusEnum.offline.getStatus()));
        bigDataPO.setSex(RandomUtil.randomInt(0, 100) > 50 ? 0 : 1);
        bigDataPO.setPhone(RandomUtil.randomNumbers(13));
        bigDataMapper.insert(bigDataPO);
        Page<BigDataPO> bigDataPOPage = bigDataMapper.selectPage(new Page<>(0, 5), new QueryWrapper<>());
        for (BigDataPO record : bigDataPOPage.getRecords()) {
            System.err.println(record);
        }
        return "ok";
    }

    /**
     * 调用service层方法, 在方法上通过注解的方式实现自动绑定数据
     * @return
     */
    @GetMapping("/test3")
    public String test3() {
        List<BigDataPO> bigDataPOS = fieldBindTestService.listBigDataPo();
        for (BigDataPO record : bigDataPOS) {
            System.err.println(record);
        }
        return "ok";
    }

    @GetMapping("/test4")
    public String test4() {
        BigDataPO bigDataPO = fieldBindTestService.getBigDataPO();
        System.out.println(bigDataPO);
        return "ok";
    }


}
